package com.yx.coredata.config;

import com.yx.coredata.common.CORSFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;

import java.util.Arrays;

/**
 * Filter配置
 *
 * @author LingDu
 * @version 1.0
 */
@Configuration
public class FilterConfig {

    //跨域filter
    @Bean
    public FilterRegistrationBean CORSFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();

        CORSFilter filter = new CORSFilter();
        registrationBean.setFilter(filter);

        registrationBean.setUrlPatterns(Arrays.asList(new String[]{"/*"}));

        return registrationBean;
    }

    //shiroFilter
    @Bean
    public FilterRegistrationBean<DelegatingFilterProxy> shiroFilterRegistration() {
        FilterRegistrationBean<DelegatingFilterProxy> registration = new FilterRegistrationBean<>();
        registration.setFilter(new DelegatingFilterProxy("shiroFilter"));
        //该值缺省为false，表示生命周期由SpringApplicationContext管理，设置为true则表示由ServletContainer管理
        registration.addInitParameter("targetFilterLifecycle", "true");
        registration.setEnabled(true);
        registration.setOrder(Integer.MAX_VALUE - 1);
        registration.addUrlPatterns("/*");
        return registration;
    }

}
